<?php
/**
 * Description:微信公众号接入调试
 * User: wh
 * Date: 2017-07-25
 * Time: 10:04
 */
namespace app\wxbackend\controller;

use app\wxbackend\controller\Wx\WxCrypt\WXBizMsgCrypt;
use app\wxbackend\controller\Wx\WxMenu;
use app\wxbackend\controller\Wx\WxRequest;
use app\wxbackend\controller\Wx\WxScene;
use app\wxbackend\controller\Wx\WxTags;
use app\wxbackend\controller\Wx\WxUser;
use think\Controller;


class WxLoadController extends Controller
{
    public function __construct()
    {

    }
    public function run()
    {
        if (isset($_GET["echostr"])) {
            //是否是验证信息
            $this->valid();
        } else {
            //其他消息处理
            $this->handleRequest();
        }
    }
    //检测验证信息
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce     = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    //处理其他类型的信息
    public function handleRequest()
    {
        $msgSignature = $_GET["msg_signature"];
        $timestamp = $_GET["timestamp"];
        $nonce     = $_GET["nonce"];
        $encryptType = (isset($_GET['encrypt_type']) && ($_GET['encrypt_type'] == 'aes')) ? "aes" : "raw";

        $request = file_get_contents('php://input');
        //$request = $GLOBALS['HTTP_RAW_POST_DATA'];
        if (!$request) {
            zft_log('wx/data','no data');
            echo '';
            exit;
        }
        //接受微信端的消息，解密
        if ($encryptType == 'aes') {
            $pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, APPID);
            $decryptMsg = '';
            $errCode = $pc->decryptMsg($msgSignature, $timestamp, $nonce, $request, $decryptMsg);
            $request = $decryptMsg;
            zft_log('wx/decrypt', $errCode);
            zft_log('wx/decrypt', json_encode($request));
        }

        $sendRequest = (array) simplexml_load_string($request, 'SimpleXMLElement', LIBXML_NOCDATA);
        $sendRequest = array_change_key_case($sendRequest, CASE_LOWER);
        $res = WxRequest::handle($sendRequest);

        //加密完发送给微信端
        if ($encryptType == 'aes') {
            $encryptMsg = '';
            $errCode2 = $pc->encryptMsg($res, $timestamp, $nonce, $encryptMsg);
            $res = $encryptMsg;
            zft_log('wx/encrypt', $errCode2);
            zft_log('wx/encrypt', json_encode($res));
        }

        echo $res;
    }

    //设置菜单
    public function setMenu()
    {
        WxMenu::setMenu();
    }

    //删除菜单
    public function deleteMenu()
    {
        WxMenu::deleteMenu();
    }

    //获取用户的所有标签
    public function getTags()
    {
        $res= WxTags::getAllTags();
        dd($res);
    }

    public function getUserList()
    {
        $res = WxUser::getList();
        dd($res);
    }

    public function getUser()
    {
        $openId = 'o2-PovlSyfXsMef-ACxHDVgmBruw';
        WxUser::getUserInfo($openId);
    }

    public function setTenantmenu()
    {
        WxMenu::setTenantMenu();
    }

    public function setOwnerMenu()
    {
        WxMenu::setOwnerMenu();
    }

    public function test()
    {
        $openId = 'o2-PovnAE_kYfb_Xiobw4lhgM7k4';
        WxMenu::getUserMenu($openId);
    }

    public function getMenu()
    {
        WxMenu::getMenu();
    }

    public function setTarget()
    {
//        $openId = ['o2-PovnAE_kYfb_Xiobw4lhgM7k4'];
//        $tagid=103;
//        WxTags::setTags($openId, $tagid);
        WxTags::setTags(['o2-PovnAE_kYfb_Xiobw4lhgM7k4'], 102);
    }

    public function cancelTarget()
    {
        $openId = ['o2-PovnAE_kYfb_Xiobw4lhgM7k4'];
        $tagid=102;
        WxTags::cancelTags($openId, $tagid);
    }

    public function setDefaultTags()
    {
        $name = '业主组';
        WxTags::setDefaultTags($name);
    }

    public function deleteTenantMenu()
    {
        $id= ['416395696'];
        foreach ($id as $v) {
            WxMenu::deleteTraditionMenu($v);
        }

    }

    public function getScene()
    {
        $qrcode = [
            'expire_seconds' => 1800,
            'action_name'    => "QR_SCENE",
            'action_info' => [
                'scense' => [
                    'scense_id' => 1000
                ]
            ]
        ];
        $jsonQr = json_encode($qrcode);
        $res = WxScene::getScene($jsonQr);
        if (!$res) {
            echo '生成失败';die;
        } else {
            $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$res['ticket'];
            var_dump($url);
            //include ROOT_PATH.'extend/Tools/phpqrcode/phpqrcode.php';
            //\QRcode::png($res['url']);
            //exit();
        }
    }

}